<?php

/**
 * uniBoard Register Page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_register{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our Templates
	 *
	 * @var object
	 */
	
	private $templates		= NULL;
	
	/**
	 * Our User API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Our ReCaptcha
	 *
	 * @var object
	 */
	
	private $recaptcha		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language File
		$appCore -> loadLanguageFile( 'users');
				
		// User Logged In?
		if ( $appCore -> user['id'] > 0)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_users'], $appCore -> lang['board_register_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents();
		}
		else if ( $appCore -> banned_ip)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_ip'], $appCore -> lang['board_register_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents( true);
		}
		else if ( !$appCore -> conf['users_register_turn'])
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_disabled'], $appCore -> lang['board_register_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents();
		}
		else
		{	
			// Block login
			$appCore -> output -> show_login = false;
					
			// Load Templates
			$this -> templates = new tpl_register_page( $appCore);
			
			// Start API
			$this -> api = new api_user( $appCore);
			
			// Give API basic setup
			$this -> api -> setUserTimezone( $appCore -> time_zone);
			$this -> api -> setUserDST( $appCore -> time_dst);
			
			$this -> api -> setUserLanguage( $appCore -> user['language']);
			$this -> api -> setUserStyle( $appCore -> user['style']);
			
			// Start ReCAPTCHA?
			if ( $appCore -> conf['recaptcha_on_register'])
			{
				$this -> recaptcha = new class_recaptcha();
			}
			
			// Run Controller
			switch ( $appCore -> _REQ( 'step'))
			{
				// Activation Details Page
				case 4:
					$this -> activate();
				break;
				// Fill Api with data and if its all-right, redirect user... somewhere
				case 3:
					$this -> finalise();
				break;
				// Check if Form was accepted, if so, draw next step
				case 2:
					if ( $appCore -> _REQ( 'rules_accept') == 1)
					{
						// Draw Registration Form
						$this -> drawForm();
					}
					else
					{
						// Set Error
						$appCore -> output -> setEvent( $appCore -> lang['board_register_rules_not_accepted'], $appCore -> lang['board_register_rules_title'], 0);
						
						// Repeat Rules
						$this -> drawGuidelines();
					}
				break;
				
				// Draw Register Guidelines
				default:
					$this -> drawGuidelines();
				break;
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Registration Rules
	 *
	 */
	
	function drawGuidelines(){
		
		// Draw Guidelines template
		$this -> appCore -> output -> draw( $this -> appCore -> lang['board_nav_register'], array(
			array( $this -> appCore -> niceLink( 'act=register', $this -> appCore -> lang['board_nav_register']),  $this -> appCore -> lang['board_nav_register'])
		), $this -> templates -> tpl_register_rules());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Registration Rules
	 *
	 */
	
	function drawForm(){
		
		// Template Vars
		$tpl_vars = array();
		
		// Action CODE-KEY pair
		$tpl_vars['act_code'] = $this -> appCore -> session -> session_code;
		$tpl_vars['act_key'] = $this -> appCore -> session -> session_key;
		
		// Show reCAPTCHA?
		if ( $this -> appCore -> conf['recaptcha_on_register'])
		{
			$tpl_vars['show_captcha'] = $this -> appCore -> session -> session_code;
			$tpl_vars['captcha_html'] = $this -> recaptcha -> recaptcha_get_html( $this -> appCore);
		}
		
		// FIELD: User Name
		$tpl_vars['user_name'] = $this -> api -> user_name;
		$tpl_vars['user_name_error'] = ( isset( $this -> api -> errors['ERROR_NAME']) ? $this -> api -> errors['ERROR_NAME'] : '');
		
		// FIELD: User Login
		$tpl_vars['user_login'] = $this -> api -> user_login;
		$tpl_vars['user_login_error'] = ( isset( $this -> api -> errors['ERROR_LOGIN']) ? $this -> api -> errors['ERROR_LOGIN'] : '');
		
		// FIELD: User Password
		$tpl_vars['user_password'] = $this -> appCore -> _POST( 'user_password');
		$tpl_vars['user_password_error'] = ( isset( $this -> api -> errors['ERROR_PASSWORD']) ? $this -> api -> errors['ERROR_PASSWORD'] : '');
		
		// FIELD: User Password Repeat
		$tpl_vars['user_password_repeat'] = $this -> appCore -> _POST( 'user_password_repeat');
		$tpl_vars['user_password_repeat_error'] = ( isset( $this -> api -> errors['ERROR_PASSWORD_REPEAT']) ? $this -> api -> errors['ERROR_PASSWORD_REPEAT'] : '');
		
		// FIELD: User E-Mail
		$tpl_vars['user_email'] = $this -> api -> user_email;
		$tpl_vars['user_email_error'] = ( isset( $this -> api -> errors['ERROR_EMAIL']) ? $this -> api -> errors['ERROR_EMAIL'] : '');
		
		// FIELD: User E-Mail Repeat
		$tpl_vars['user_email_repeat'] = $this -> appCore -> _POST( 'user_email_repeat');
		$tpl_vars['user_email_repeat_error'] = ( isset( $this -> api -> errors['ERROR_EMAIL_REPEAT']) ? $this -> api -> errors['ERROR_EMAIL_REPEAT'] : '');
		
		// FIELDS: MAIL SETTINGS
		$tpl_vars['user_want_emails'] = $this -> api -> user_want_emails;
		$tpl_vars['user_show_email'] = $this -> api -> user_show_email;
		$tpl_vars['user_want_mailings'] = $this -> api -> user_want_mailings;
		
		// FIELD: Timezone
		$tpl_vars['timezones_list'] = $this -> appCore -> drawRawList( $this -> appCore -> getTimezones(), $this -> api -> user_timezone);
		$tpl_vars['user_dst'] = $this -> api -> user_dst;
		
		// Draw Template
		$this -> appCore -> output -> draw( $this -> appCore -> lang['board_nav_register'], array(
			array( $this -> appCore -> niceLink( 'act=register', $this -> appCore -> lang['board_nav_register']),  $this -> appCore -> lang['board_nav_register'])
		), $this -> templates -> tpl_register_form( $tpl_vars));
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Finalises registration process or throws user back to registration form
	 *
	 */
	
	function finalise(){
		
		// Fill API with data
		$this -> api -> setUserName( $this -> appCore -> _POST( 'user_name'));
		$this -> api -> setUserLogin( $this -> appCore -> _POST( 'user_login'));
		$this -> api -> setUserPassword( $this -> appCore -> _POST( 'user_password'));
		$this -> api -> setUserEmail( $this -> appCore -> _POST( 'user_email'));
		$this -> api -> setUserWantEmails( $this -> appCore -> _POST( 'user_want_emails'));
		$this -> api -> setUserShowEmail( $this -> appCore -> _POST( 'user_show_email'));
		$this -> api -> setUserWantMailings( $this -> appCore -> _POST( 'user_want_mailings'));
		$this -> api -> setUserTimezone( $this -> appCore -> _POST( 'user_timezone'));
		$this -> api -> setUserDST( $this -> appCore -> _POST( 'user_dst'));
		
		// CHECK REPEAT: Password
		if ( $this -> appCore -> _POST( 'user_password') != $this -> appCore -> _POST( 'user_password_repeat') || strlen( $this -> appCore -> _POST( 'user_password_repeat')) == 0)
		{
			$this -> api -> setError((strlen( $this -> appCore -> _POST( 'user_password_repeat')) > 0 ? $this -> appCore -> lang['user_cp_fields_different'] : $this -> appCore -> lang['user_cp_fields_both']), 'ERROR_PASSWORD_REPEAT');
		}
		
		// CHECK REPEAT: E-Mail
		if ( $this -> appCore -> _POST( 'user_email') != $this -> appCore -> _POST( 'user_email_repeat') || strlen( $this -> appCore -> _POST( 'user_email_repeat')) == 0)
		{
			$this -> api -> setError((strlen( $this -> appCore -> _POST( 'user_email_repeat')) > 0 ? $this -> appCore -> lang['user_cp_fields_different'] : $this -> appCore -> lang['user_cp_fields_both']), 'ERROR_EMAIL_REPEAT');
		}
		
		// Any errors?
		try
		{
			// API has Errors?
			if ( $this -> api -> error)
			{
				throw new exc_small( $this -> appCore -> lang['form_contains_errors'], $this -> appCore -> lang['board_nav_register']);
			}
			
			// Captcha is invalid?
			if ( $this -> appCore -> conf['recaptcha_on_register'] && !$this -> recaptcha -> recaptcha_check())
			{
				throw new exc_small( $this -> appCore -> lang['recaptcha_incorrect_try_again'], $this -> appCore -> lang['recaptcha_incorrect']);
			}
			
			// Request is invalid?
			if ( !$this -> appCore -> validRequest())
			{
				throw new exc_small( $this -> appCore -> lang['wrong_action_auth'], $this -> appCore -> lang['wrong_action_auth_title']);
			}
			
			// Fill First and Last time on-line
			$this -> api -> setUserJoinDate( $this -> appCore -> time);
			$this -> api -> setUserJoinIp( $this -> appCore -> client_ip);
			$this -> api -> setUserLastDate( $this -> appCore -> time);
			$this -> api -> setUserLastIp( $this -> appCore -> client_ip);
			$this -> api -> setUserNameDate( $this -> appCore -> time);			
			
			// Bring Mailer to life, we gonna spam somebody with mails
			$mailer = new class_mailer( $this -> appCore);
			
			// Is new user in need of teh activation?
			if ( $this -> appCore -> conf['users_register_activation'] == 2) // Admin gotta move his butt to activate user
			{
				// Set Activation Type to 2
				$this -> api -> setUserActivation( 2);
				
				// Set User Group to 4 (Waiting for activation)
				$this -> api -> setGroupId( 4);
				$this -> api -> setUserGroups( array(4));
				
				// Save Api
				$this -> api -> saveData();
				
				// Make him member of inactive dudes
				$this -> appCore -> users -> makeMember( 4, $this -> api -> user_name);
				
				// Update Latest user
				$this -> appCore -> users -> setLatestUser( $this -> api -> user_id, $this -> api -> user_name);
				
				// Redirect to "done" page for admin-side activation
				$this -> appCore -> setRedirect( $this -> appCore -> niceLink('act=register&step=4&adm=1', $this -> appCore -> lang['board_nav_register']), false);
				
				// Keep Script Alive
				$this -> appCore -> stayAlive();
				
				// Send Email
				$mailer -> sendEmail(
					$this -> api -> user_email,
					$this -> api -> user_name,
					$this -> appCore -> lang['email_register_title'],
					class_strings::parseMailText( file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> user_language . '/email_register_2.txt'), array(
						'{USER_NAME}' 			=> $this -> api -> user_name,
						'{USER_LOGIN}' 			=> $this -> api -> user_login,
						'{USER_PASSWORD}' 		=> $this -> appCore -> _POST( 'user_password'),
						'{BOARD_NAME}' 			=> $this -> appCore -> conf['board_name'],
						'{BOARD_LOGIN_URL}' 	=> $this -> appCore -> emailLink( 'act=login', $this -> appCore -> lang['login_form_login']),
						'{BOARD_URL}' 			=> $this -> appCore -> emailLink()
					)));
			}
			else if ( $this -> appCore -> conf['users_register_activation'] == 1) // User will have to check his @ for activation code
			{
				// Set Activation Type to 1
				$this -> api -> setUserActivation( 1);
				
				// Generate Activation Code
				$this -> api -> setUserCode();
				
				// Set User Group to 4 (Waiting for activation)
				$this -> api -> setGroupId( 4);
				$this -> api -> setUserGroups( array(4));
				
				// Save Api
				$this -> api -> saveData();
				
				// Make him member of inactive dudes
				$this -> appCore -> users -> makeMember( 4, $this -> api -> user_name);
				
				// Update Latest user
				$this -> appCore -> users -> setLatestUser( $this -> api -> user_id, $this -> api -> user_name);
				
				// Redirect to "done" page for user-side activation
				$this -> appCore -> setRedirect( $this -> appCore -> niceLink('act=register&step=4&adm=0', $this -> appCore -> lang['board_nav_register']), false);
				
				// Keep Script Alive
				$this -> appCore -> stayAlive();
				
				// Send Email
				$mailer -> sendEmail(
					$this -> api -> user_email,
					$this -> api -> user_name,
					$this -> appCore -> lang['email_register_title'],
					class_strings::parseMailText( file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> user_language . '/email_register_1.txt'), array(
						'{USER_NAME}' 						=> $this -> api -> user_name,
						'{USER_LOGIN}' 						=> $this -> api -> user_login,
						'{USER_PASSWORD}' 					=> $this -> appCore -> _POST( 'user_password'),
						'{USER_ACTIVATION_CODE}' 			=> $this -> api -> user_code,
						'{BOARD_NAME}' 						=> $this -> appCore -> conf['board_name'],
						'{BOARD_ACTIVATION_URL}' 			=> $this -> appCore -> emailLink( 'act=activate&user=' . $this -> api -> user_id . '&code=' . $this -> api -> user_code, $this -> appCore -> lang['board_nav_activation']),
						'{BOARD_ACTIVATION_MANUAL_URL}' 	=> $this -> appCore -> emailLink( 'act=activate&user=' . $this -> api -> user_id, $this -> appCore -> lang['board_nav_activation']),
						'{BOARD_LOGIN_URL}' 				=> $this -> appCore -> emailLink( 'act=login', $this -> appCore -> lang['login_form_login']),
						'{BOARD_URL}' 						=> $this -> appCore -> emailLink()
					)));
			}
			else // Login user immediately and send summary mail
			{
				// Set Activation Type to 0
				$this -> api -> setUserActivation( 0);
				
				// Set User Group to 3 (registered)
				$this -> api -> setGroupId( 3);
				$this -> api -> setUserGroups( array(3));
				
				// Save Api
				$this -> api -> saveData();
				
				// Make him member of inactive dudes
				$this -> appCore -> users -> makeMember( 4, $this -> api -> user_name);
				
				// Update Latest user
				$this -> appCore -> users -> setLatestUser( $this -> api -> user_id, $this -> api -> user_name);
				
				// LogIn our new member!
				$this -> appCore -> session -> updateSession( $this -> api -> user_id);
				
				// Trow him to Board Index
				$this -> appCore -> output -> drawRedirect( $this -> appCore -> link(), $this -> appCore -> lang['board_register_complete_activation_0']);
				
				// Keep Script Alive
				$this -> appCore -> stayAlive();
				
				// Send E-Mail
				$mailer -> sendEmail(
					$this -> api -> user_email,
					$this -> api -> user_name,
					$this -> appCore -> lang['email_register_title'],
					class_strings::parseMailText( file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> user_language . '/email_register_0.txt'), array(
						'{USER_NAME}' 			=> $this -> api -> user_name,
						'{USER_LOGIN}' 			=> $this -> api -> user_login,
						'{USER_PASSWORD}' 		=> $this -> appCore -> _POST( 'user_password'),
						'{BOARD_NAME}' 			=> $this -> appCore -> conf['board_name'],
						'{BOARD_LOGIN_URL}' 	=> $this -> appCore -> emailLink( 'act=login', $this -> appCore -> lang['login_form_login']),
						'{BOARD_URL}' 			=> $this -> appCore -> emailLink()
					)));
			}
		}
		catch ( exc_small $exception)
		{
			// Set Error
			$this -> appCore -> output -> setEvent( $exception -> message, $exception -> title, 0);
			
			// Throw us back to form
			$this -> drawForm();
		}
				
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws page with details about Account Activation
	 *
	 */
	
	function activate(){
		
		// Draw Template
		$this -> appCore -> output -> draw( $this -> appCore -> lang['board_nav_register'], array(
			array( $this -> appCore -> niceLink( 'act=register', $this -> appCore -> lang['board_nav_register']),  $this -> appCore -> lang['board_nav_register'])
		), $this -> templates -> tpl_register_done(array( 'by_admin' => ($this -> appCore -> _REQ( 'adm') == 1))));
		
	}
	
}